package com.example.administrator.easy.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.easy.R;
import com.example.administrator.easy.detailxinfo.DetailBean;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by jingsheng on 2016/11/14.
 */

public class DetailsAdapter extends BaseAdapter {//

    private Context context;
    private List<DetailBean > list;

    private ImageOptions options;
    public DetailsAdapter(Context context, List<DetailBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        options=new ImageOptions.Builder()
                .setCircular(true)
                .setLoadingDrawableId(R.mipmap.default_circle_img)
                .build();
        DetailsViewHoder vh;
        if (convertView==null){
            vh=new DetailsViewHoder();
            convertView=View.inflate(context,R.layout.detail_discover_dialogue,null);
            vh.setView(convertView);
            convertView.setTag(vh);
        }else {
            vh= (DetailsViewHoder) convertView.getTag();
        }

        vh.tvUserName.setText(list.get(position).userName);
        vh.tvCount.setText(list.get(position).count);

        x.image().bind(vh.ivUser,list.get(position).urlUser,options);

        if (TextUtils.isEmpty(list.get(position).urlCount)){
            vh.ivCount.setVisibility(View.GONE);
        }else {
            x.image().bind(vh.ivCount,list.get(position).urlCount);
        }

        return convertView;
    }

    class DetailsViewHoder{
        private TextView tvUserName,tvCount;
        private ImageView ivUser,ivCount;
        public void setView(View view){
            tvUserName= (TextView) view.findViewById(R.id.detail_discover_titlename);
            tvCount= (TextView) view.findViewById(R.id.detail_discover_count);
            ivUser= (ImageView) view.findViewById(R.id.detail_lv_user);
            ivCount= (ImageView) view.findViewById(R.id.detail_lv_count);
        }
    }
}
